package com.stylefeng.guns.modular.system.service.impl;

import com.stylefeng.guns.config.loadBaseData.ConfigParamContant;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.model.CustomerAddress;
import com.stylefeng.guns.modular.system.dao.CustomerAddressMapper;
import com.stylefeng.guns.modular.system.service.ICustomerAddressService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.stylefeng.guns.config.redis.RedisUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 客户地址表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2019-09-14
 */
@Service
public class CustomerAddressServiceImpl extends ServiceImpl<CustomerAddressMapper, CustomerAddress> implements ICustomerAddressService {

    @Resource
    private  CustomerAddressMapper customerAddressMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public ResponseResultDTO chekCarPrice(String alat, String along, String clat, String clong) {
        ResponseResultDTO resultDTO = new ResponseResultDTO();
        try {
            //查询距离
            String distance = customerAddressMapper.chekCarPrice(alat,along,clat,clong);
            //查询配置
            String sdistance = redisUtil.get(ConfigParamContant.S_DISTANCE).toString();   //起步距离
            String sprice =  redisUtil.get(ConfigParamContant.S_PRICE).toString();  //起步价
            String lprice =  redisUtil.get(ConfigParamContant.L_PRICE).toString();  //美多一公里 价格

            Double price ;
            if(Double.parseDouble(distance)<=Double.parseDouble(sdistance)){  //小于起步距离 安起步价算
                price = Double.parseDouble(sprice);
            }else{  //大于起步距离    起步价 + 公里价格*多出公里
                price = Double.parseDouble(sprice) + (int) Math.ceil(Double.parseDouble(distance)-Double.parseDouble(sdistance)) * Double.parseDouble(lprice);
            }
            Map<String,Object> result = new HashMap<String,Object>();
            result.put("distance",distance);
            result.put("price",price);
            resultDTO.setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            resultDTO.setMsg("系统异常");
            resultDTO.setStatus("-1");
        }
        return resultDTO;
    }


}
